﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RegionInfo
{
	public GameObject obj;
	public UIWidget region;
	public bool isCircle;
	public UILabelEx text;
	public GameObject instructionObj;
	public MeshFilter lineFilter;
	public Vector2 textPos;

	Vector3 lineP0;
	Vector3 lineP2;

	public void Destroy()
	{
		if (this.obj != null)
		{
			GameObject.Destroy(this.obj);
			this.obj = null;
			this.region = null;

			if (this.text != null)
			{
				GameObject.Destroy(this.text.gameObject);
				this.text = null;
			}

			if (this.instructionObj != null)
			{
				GameObject.Destroy(this.instructionObj);
				this.instructionObj = null;
			}
		}
		else if(this.region != null)
		{
			GameObject.Destroy(this.region.gameObject);
			this.region = null;
		}

		if (this.lineFilter != null)
		{
			GameObject.Destroy(this.lineFilter);
			this.lineFilter = null;
		}
	}

	public UILabelEx GetLabel()
	{
		if (this.text != null)
		{
			return this.text;
		}

		if (this.instructionObj != null)
		{
			return this.instructionObj.GetComponentInChildren<UILabelEx>();
		}

		return null;
	}

	public void UpdateTextPosition()
	{
		if (this.text != null)
		{
			Vector3[] worldCorners = region.worldCorners;
			Vector3 lbCorner = worldCorners[0], rtCorner = worldCorners[2];
			RestrictControlRegionManager.FixCorner(ref lbCorner, ref rtCorner);

			Vector2 size = text.printedSize;
			size = text.transform.TransformVector(size);

			lbCorner -= (Vector3)size / 2;
			rtCorner += (Vector3)size / 2;
			Vector3 pos = new Vector3(Mathf.LerpUnclamped(lbCorner.x, rtCorner.x, textPos.x), Mathf.LerpUnclamped(lbCorner.y, rtCorner.y, textPos.y), 0);

			text.transform.position = pos;


//			Vector3 worldPos = region.transform.TransformPoint(textPos);
//			Vector3 localPos = text.transform.parent.InverseTransformPoint(worldPos);
//			text.transform.localPosition = localPos;
		}
		else if(this.instructionObj != null)
		{
			Vector3 worldPos = region.transform.TransformPoint(textPos);
			Vector3 localPos = instructionObj.transform.parent.InverseTransformPoint(worldPos);
			instructionObj.transform.localPosition = localPos;
		}
	}

	public void UpdateLine()
	{
		if (this.text == null)
		{
			return;
		}

		Vector3[] worldCorners = region.worldCorners;
		Vector3 lbCorner = worldCorners[0];
		Vector3 rtCorner = worldCorners[2];
		Vector3 center = (lbCorner + rtCorner) / 2;

		int minIndex = -1;
		float minLen = float.MaxValue;
		Vector3[] textWorldCorners = text.worldCorners;
		for (int i = 0; i < textWorldCorners.Length; i += 3)
		{
			Vector3 corner = textWorldCorners[i];
			float curLen = (center - corner).magnitude;
			if (curLen < minLen)
			{
				minLen = curLen;
				minIndex = i;
			}
		}

		Vector3 minCorner = textWorldCorners[minIndex];
		Vector3 maxCorner = textWorldCorners[minIndex == 0? 3: 0];

		Vector3 intersect = IntersectPoint(minCorner);
		Vector3 midPoint = new Vector3(intersect.x, minCorner.y, 0);

		if (lineP0 == intersect && lineP2 == minCorner)
		{
			return;
		}

		lineP0 = intersect;
		lineP2 = minCorner;

		intersect = this.lineFilter.transform.InverseTransformPoint(intersect);
		midPoint = this.lineFilter.transform.InverseTransformPoint(midPoint);
		minCorner = this.lineFilter.transform.InverseTransformPoint(minCorner);
		maxCorner = this.lineFilter.transform.InverseTransformPoint(maxCorner);



		float len0 = (midPoint - intersect).magnitude;
		float len1 = (midPoint - minCorner).magnitude;
		float a = len0;
		float d = len1;
		float s = d;
		if (a < d)
		{
//			float temp = d;
//			d = a;
//			a = temp;

			s = (d * d + a * a) / (2 * d);

			Vector3 newMidPoint = minCorner + s * (midPoint - minCorner).normalized;
			if (len1 < len0)
			{
				newMidPoint = intersect + s * (midPoint - intersect).normalized;
			}
			midPoint = newMidPoint;
		}



//		float curveHalfLength = Mathf.Min(50, s * 0.9f);
		float curveHalfLength = s * 0.9f;

		Vector3 midPoint0 = (intersect - midPoint).normalized * curveHalfLength + midPoint;
		Vector3 midPoint2 = (minCorner - midPoint).normalized * curveHalfLength + midPoint;

//		Vector3 midPoint0 = midPoint;
//		Vector3 midPoint2 = midPoint;

//		intersect = new Vector3(-100, 0);
//		midPoint0 = new Vector3(0, 0);
//		midPoint = new Vector3(100, 0);
//		midPoint2 = new Vector3(100, 100);
//		maxCorner = new Vector3(100, 200);
//
//		Debug.LogError(intersect + "  " + midPoint + "  " + minCorner);

		Mesh mesh = this.lineFilter.sharedMesh;
		mesh = BezierMesh(mesh, intersect, midPoint0, midPoint, midPoint2, maxCorner);
		this.lineFilter.sharedMesh = mesh;
	}

	public Vector3 IntersectPoint(Vector3 p0)
	{
		Vector3[] worldCorners = region.worldCorners;
		Vector3 lbCorner = worldCorners[0];
		Vector3 rtCorner = worldCorners[2];
		Vector3 center = (lbCorner + rtCorner) / 2;

		Vector3 d = p0 - center;

		Vector3 extend = (rtCorner - lbCorner) / 2;
		extend.x = Mathf.Abs(extend.x);
		extend.y = Mathf.Abs(extend.y);

		if (this.isCircle)
		{
			float angle = Vector3.Angle(Vector3.right, d);
			Vector3 z = Vector3.Cross(Vector3.right, d);
			if (Vector3.Dot(z, Vector3.forward) < 0)
			{
				angle = -angle;
			}

			return new Vector3(extend.x * Mathf.Cos(angle * Mathf.Deg2Rad), extend.y * Mathf.Sin(angle * Mathf.Deg2Rad), 0) + center;
		}
		else
		{
			d.y *= extend.x / extend.y;
			float scale0 = extend.x / Mathf.Abs(d.x);
			float scale1 = extend.x / Mathf.Abs(d.y);
			float scale = Mathf.Min(scale0, scale1);
			d *= scale;
			d.y *= extend.y / extend.x;
			return d + center;
		}
	}


	Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float t)
	{
		float oneMinusT = 1 - t;
		Vector3 p = oneMinusT * oneMinusT * p0 + 2 * oneMinusT * t * p1 + t * t * p2;
		return p;
	}

	Vector3 BezierTangent(Vector3 p0, Vector3 p1, Vector3 p2, float t)
	{
		float oneMinusT = 1 - t;
		Vector3 p = -2 * oneMinusT * p0 + (2 - 4 * t) * p1 + 2 * t * p2;
		return p;
	}

	void OffsetPointByWidth(Vector3 p, Vector3 tangent, float width, out Vector3 pn0, out Vector3 pn1)
	{
		Quaternion q = Quaternion.Euler(0, 0, 90);
		Vector3 normal = q * tangent;
		normal.Normalize();

		pn0 = p + width * normal;
		pn1 = p - width * normal;
	}

	Mesh BezierMesh(Mesh mesh, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4)
	{
		if(mesh == null)
		{	
			mesh = new Mesh();
		}

		float width = 5;
		int max = 10;


		int headVectNum = 3;

		Color c = new Color(1, 1, 1, 1);
		Color c2 = new Color(1, 1, 1, 0);
		Vector3[] verts = new Vector3[(max + 1) * 3 + headVectNum * 2];
		Color[] colors = new Color[(max + 1) * 3 + headVectNum * 2];
		for (int i = 0; i <= max; i++)
		{
			float t = (float)i / max;
			Vector3 p = Bezier(p1, p2, p3, t);
			Vector3 tangent = BezierTangent(p1, p2, p3, t);
			if (tangent.magnitude < 0.01f)
			{
				tangent = p4 - p1;
			}

			Vector3 pn0 = Vector3.zero;
			Vector3 pn1 = Vector3.zero;
			OffsetPointByWidth(p, tangent, width, out pn0, out pn1);

			verts[headVectNum + i * 3] = pn0;
			verts[headVectNum + i * 3 + 1] = p;
			verts[headVectNum + i * 3 + 2] = pn1;

			colors[headVectNum + i * 3] = c2;
			colors[headVectNum + i * 3 + 1] = c;
			colors[headVectNum + i * 3 + 2] = c2;
		}


		{
			Vector3 pn0 = Vector3.zero;
			Vector3 pn1 = Vector3.zero;
			OffsetPointByWidth(p0, p1 - p0, width, out pn0, out pn1);

			verts[0] = pn0;
			verts[1] = p0;
			verts[2] = pn1;

			colors[0] = c2;
			colors[1] = c;
			colors[2] = c2;
		}

		{
			Vector3 pn0 = Vector3.zero;
			Vector3 pn1 = Vector3.zero;
			OffsetPointByWidth(p4, p4 - p3, width, out pn0, out pn1);

			verts[verts.Length - 3] = pn0;
			verts[verts.Length - 2] = p4;
			verts[verts.Length - 1] = pn1;

			colors[verts.Length - 3] = c2;
			colors[verts.Length - 2] = c;
			colors[verts.Length - 1] = c2;
		}


		int[] indices = new int[(max + 2) * 2 * 2 * 3];
		for (int i = 0; i < max + 2; i++)
		{
			int vertIndex0 = i * 3;
			int vertIndex1 = (i + 1) * 3;

			int index0 = i * 2 * 2 * 3;
			for (int j = 0; j < 2; j++)
			{
				indices[index0 + j * 6] = vertIndex0 + j;
				indices[index0 + j * 6 + 1] = vertIndex1 + j;
				indices[index0 + j * 6 + 2] = vertIndex0 + j + 1;

				indices[index0 + j * 6 + 3] = vertIndex0 + j + 1;
				indices[index0 + j * 6 + 4] = vertIndex1 + j;
				indices[index0 + j * 6 + 5] = vertIndex1 + j + 1;
			}
		}



		mesh.vertices = verts;
		mesh.colors = colors;
		mesh.triangles = indices;

		mesh.RecalculateBounds();
		mesh.RecalculateNormals();

		return mesh;
	}
}

public class RestrictControlRegionManager : MonoBehaviour
{
    private const byte CLICK_MASK = 1;
    private const byte DRAG_MASK = 2;
    private const byte PRESS_DOWN_MASK = 4;

    public readonly static byte CONTROL_CLICK = CLICK_MASK;
    public readonly static byte CONTROL_DRAG = DRAG_MASK;
    public readonly static byte CONTROL_CLICK_DRAG = (byte)(CLICK_MASK | DRAG_MASK);
    public readonly static byte CONTROL_PRESS_DOWN = PRESS_DOWN_MASK;

	public GameObject instructionObj;
    public UILabelEx instructionLabel;
    public Object skipBtn;
	public GameObject nextStepBtn;

	public UIWidget region_bg;
	public UIWidget region_black;
	public UIWidget region;
	public UILabelEx regionText;
	public UIPanel uiPanel;
	public MeshFilter lineFilter;
	public UIWidget block;
	public ControlRegion controlRegion;
	public GameObject customControlObj;
	public UILabelEx guidanceLab;
	public GameObject guidanceBg;

	private GameObject effectObj;
	GameObject locationObj;
	GameObject controlObj;
    bool customControl;
	System.Func<GameObject, bool> finishCallback;
	Dictionary<GameObject, RegionInfo> regionMap = new Dictionary<GameObject, RegionInfo>();
	UIWidget leftBlock;
	UIWidget rightBlock;
	UIWidget topBlock;
	UIWidget bottomBlock;
	UIPanel panel;
	Material uiBlockMat;
	Vector3 oldLbCorner;
	Vector3 oldRtCorner;
    bool isDraged;

	void Awake()
	{
		this.instructionObj.SetActive(false);
		this.region.enabled = false;
		this.regionText.enabled = false;
		this.lineFilter.gameObject.SetActive(false);
		this.customControlObj.SetActive(false);
		EnableBlock(false);
	}

	void Update()
	{
		Dictionary<GameObject, RegionInfo>.Enumerator enumerator = this.regionMap.GetEnumerator();
		while (enumerator.MoveNext())
		{
			UpdateRegion(enumerator.Current.Key, enumerator.Current.Value.region);
			enumerator.Current.Value.UpdateTextPosition();
			enumerator.Current.Value.UpdateLine();
		}

        if (locationObj)
        {
            UIWidget widget = locationObj.GetComponent<UIWidget>();
            Vector3 lbCorner, rtCorner;
            GetWidgetLocalCorners(widget, out lbCorner, out rtCorner);
            UpdateControlRegion(lbCorner, rtCorner, finishCallback == null);
        }
        else
        {
            UpdateControlRegion(Vector3.zero, Vector3.zero, true);
        }
	}

	public void ShowInstruction(bool show, Vector3 pos)
	{
		this.instructionObj.transform.localPosition = pos;
		this.instructionObj.SetActive(show);
//		if (show)
//		{
//			return this.instructionObj.GetComponentInChildren<UILabelEx>();
//		}
//
//		return null;
	}

	public RegionInfo AddRegion(GameObject obj, bool circle, bool show)
	{
		RegionInfo regionInfo = null;
		if (!this.regionMap.TryGetValue(obj, out regionInfo))
		{
			regionInfo = new RegionInfo();
			regionInfo.isCircle = circle;

			bool accurate = true;
			if (accurate)
			{
				regionInfo.obj = GameObject.Instantiate(obj);
				List<Collider2D> colliders = regionInfo.obj.GetComponentsInChildrenEx<Collider2D>();
				for (int i = 0; i < colliders.Count; i++)
				{
					colliders[i].enabled = false;
				}
                colliders.RecycleToListPool();

				regionInfo.region = regionInfo.obj.GetComponent<UIWidget>();
				regionInfo.region.SetAnchor((Transform)null);

				regionInfo.obj.transform.SetParent(this.uiPanel.transform, false);
				List<UIWidget> widgets = regionInfo.obj.GetComponentsInChildrenEx<UIWidget>();
                for (int i = 0; i < widgets.Count; i++)
				{
					if (widgets[i] is UILabel)
					{
						widgets[i].enabled = false;
					}
					else if (widgets[i] is UI2DSpriteEx)
					{
						widgets[i].shader = this.region.shader;
					}
				}
                widgets.RecycleToListPool();

				if (show)
				{
//					regionInfo.instructionObj = GameObject.Instantiate(this.instructionObj);
//					regionInfo.instructionObj.transform.SetParent(this.uiPanel.transform, false);
//					regionInfo.instructionObj.SetActive(true);
				}
				else
				{
					regionInfo.text = GameObject.Instantiate(this.region.GetComponentInChildren<UILabelEx>());
					regionInfo.text.transform.SetParent(this.uiPanel.transform, false);
					regionInfo.text.enabled = true;
				}
			}
			else
			{
				regionInfo.region = GameObject.Instantiate(this.region, this.region.transform.parent);
				regionInfo.region.transform.SetParent(this.uiPanel.transform, false);
				regionInfo.region.enabled = show;
				if (circle)
				{
					UI2DSpriteEx spriteEx = regionInfo.region as UI2DSpriteEx;
					Material material = new Material(spriteEx.shader);
					material.SetFloat("_Circle", 1);
					spriteEx.material = material;
				}
				regionInfo.text = regionInfo.region.GetComponentInChildren<UILabelEx>();
				regionInfo.text.enabled = true;
			}

			regionInfo.lineFilter = GameObject.Instantiate(this.lineFilter, this.lineFilter.transform.parent);
			regionInfo.lineFilter.gameObject.SetActive(true);

			this.regionMap.Add(obj, regionInfo);
		}

		return regionInfo;
	}

	public void ShowFinger(bool show)
	{
		if (this.effectObj != null)
		{
			show = false;
		}

		this.controlRegion.gameObject.SetActive(show);
	}

    void EnableCustomControlObj(bool enable)
    {
        if (enable)
        {
            UIWidget widget = customControlObj.GetComponent<UIWidget>();
            customControlObj.SetActive(true);
            widget.SetAnchor(locationObj, 0, 0, 0, 0);
            widget.ResizeCollider();
        }
        else
        {
            customControlObj.SetActive(false);
        }
    }

    public void RestrictControl(GameObject _controlObj, GameObject _controlRegionObj, string effectPath, byte controlMask, bool _customControl, System.Func<GameObject, bool> finishCallback)
	{
        if (_controlObj == null)
        {
            return;
        }

        if (controlMask == 0)
        {
            controlMask = CLICK_MASK;
        }

        DestroyEffectObjs();

		if (!string.IsNullOrEmpty(effectPath))
		{
			Debug.LogError("effectPath(" + effectPath + ") is 2222222");
			Object effectPrefab = ResourceManager.LoadAsset(effectPath);
			if (effectPrefab != null)
			{
				Debug.LogError("effectPath(" + effectPath + ") is 3333333");
				effectObj = GameObject.Instantiate(effectPrefab) as GameObject;
				effectObj.transform.SetParent(controlRegion.transform.parent, false);
			}
		}

		locationObj = _controlObj;
        if (_controlRegionObj != null)
        {
            locationObj = _controlRegionObj;
        }

        this.customControl = _customControl;
		if (_customControl)
		{
			controlObj = customControlObj;
            controlMask = CLICK_MASK;
		}
		else
		{
			controlObj = _controlObj;
		}

		this.finishCallback = finishCallback;

		if (this.finishCallback != null)
		{
			UISlider slider = controlObj.GetComponent<UISlider>();
			if (slider != null)
			{
				slider.onDragFinished += OnDragFinish;
			}
			else
			{
                if ((controlMask & PRESS_DOWN_MASK) != 0)
                {
                    UIEventListener.Get(controlObj).onPress += OnPressDown;
                }
                else if ((controlMask & DRAG_MASK) != 0)
				{
                    this.isDraged = (controlMask & CLICK_MASK) != 0;
					UIEventListener.Get(controlObj).onPress += OnPress;
                    UIEventListener.Get(controlObj).onDrag += OnDrag;
				}
				else
				{
					UIEventListener.Get(controlObj).onClick += OnClick;
				}
			}
			
			UIDragScrollView dragScrollView = controlObj.GetComponentInChildren<UIDragScrollView>();
			if (dragScrollView != null)
			{
				dragScrollView.enabled = false;
			}
		}
		
		EnableBlock(true);
		oldLbCorner = oldRtCorner = Vector3.zero;
		Update();

        if (this.locationObj != this.controlObj && _customControl)
		{
            EnableCustomControlObj(true);
		}
	}

	public void GetWidgetLocalCorners(UIWidget widget, out Vector3 lbCorner, out Vector3 rtCorner)
	{
//		if (widget == null || widget.panel == null)
//		{
//			lbCorner = rtCorner = Vector3.zero;
//			return;
//		}

		Vector3[] widgetWorldCorners = widget.worldCorners;
		lbCorner = GetPanel().transform.InverseTransformPoint(widgetWorldCorners[0]);
		rtCorner = GetPanel().transform.InverseTransformPoint(widgetWorldCorners[2]);

		FixCorner(ref lbCorner, ref rtCorner);
	}

	public static void FixCorner(ref Vector3 lbCorner, ref Vector3 rtCorner)
	{
		Vector3 min = Vector3.Min(lbCorner, rtCorner);
		Vector3 max = Vector3.Max(lbCorner, rtCorner);
		lbCorner = min;
		rtCorner = max;
	}

    void DestroyEffectObjs(bool onlyEffect = false)
    {
        if (!onlyEffect)
        {
            ShowInstruction(false, Vector3.zero);
        }

        foreach (var entry in regionMap)
        {
            entry.Value.Destroy();
        }
        regionMap.Clear();


        if (effectObj != null)
        {
            GameObject.Destroy(effectObj);
            effectObj = null;
        }
    }

	public void EnableBlock(bool enable)
	{
		if (!enable)
		{
            DestroyEffectObjs();
		}

		if (leftBlock == null)
		{
			leftBlock = block;
			leftBlock.MakePixelPerfect();
			rightBlock = GameObject.Instantiate(block, block.transform.parent);
			topBlock = GameObject.Instantiate(block, block.transform.parent);
			bottomBlock = GameObject.Instantiate(block, block.transform.parent);
		}
			
		this.region_bg.enabled = enable;
		this.region_black.enabled = enable;

		if (leftBlock.enabled != enable)
		{
			leftBlock.enabled = enable;
			rightBlock.enabled = enable;
			topBlock.enabled = enable;
			bottomBlock.enabled = enable;
		}

		if (effectObj != null)
		{
			if (effectObj.activeSelf != enable)
			{
				effectObj.SetActive(enable);
			}

			if (controlRegion.gameObject.activeSelf != false)
			{
				controlRegion.gameObject.SetActive(false);
			}
		}
		else
		{
			if (controlRegion.gameObject.activeSelf != enable)
			{
				controlRegion.gameObject.SetActive(enable);
			}
		}

		if (enable)
		{
            enable = locationObj != controlObj && customControl;
		}
	
		if (customControlObj.activeSelf != enable)
		{
			customControlObj.SetActive(enable);
		}
	}

    bool ControlFinish(bool force)
    {
        GameObject tempObj = this.controlObj;
        controlObj = null;

        GameObject tempLocationObj = this.locationObj;
        locationObj = null;

        System.Func<GameObject, bool> tempCallback = finishCallback;
        finishCallback = null;
        if (tempCallback != null)
        {
            if (!force && !tempCallback(tempObj))
            {
                finishCallback = tempCallback;
                controlObj = tempObj;
                locationObj = tempLocationObj;
                return false;
            }
        }

        DestroyEffectObjs(true);

        return true;
    }

	void OnDragFinish()
	{
        RemoveControlObj(false);
	}

	void OnClick(GameObject obj)
    {
        RemoveControlObj(false);
	}

    public void RemoveControlObj(bool force)
    {
        if (controlObj == null)
        {
            return;
        }

        GameObject tempControlObj = controlObj;
        if (ControlFinish(force))
        {
            UISlider slider = tempControlObj.GetComponent<UISlider>();
            if (slider != null)
            {
                slider.onDragFinished -= OnDragFinish;
            }

            UIDragScrollView dragScrollView = tempControlObj.GetComponentInChildren<UIDragScrollView>();
            if (dragScrollView != null)
            {
                dragScrollView.enabled = true;
            }

            UIEventListener.Get(tempControlObj).onPress -= OnPressDown;
            UIEventListener.Get(tempControlObj).onPress -= OnPress;
            UIEventListener.Get(tempControlObj).onDrag -= OnDrag;
            UIEventListener.Get(tempControlObj).onClick -= OnClick;
        }
    }

    void OnDrag(GameObject obj, Vector2 delta)
    {
        isDraged = true;
    }

	void OnPress(GameObject obj, bool state)
	{
		if (state)
		{
			return;
		}

        if (isDraged)
        {
            RemoveControlObj(false);
        }
	}

    void OnPressDown(GameObject obj, bool state)
    {
        if (state)
        {
            RemoveControlObj(false);
        }
    }

	UIPanel GetPanel()
	{
		if (panel == null)
		{
			panel = this.GetComponent<UIPanel>();
		}
		return panel;
	}

	void UpdateRegion(GameObject obj, UIWidget region)
	{
		if (obj == null || region == null)
		{
			return;
		}

		Vector3 lbCorner, rtCorner;
		UIWidget widget = obj.GetComponent<UIWidget>();
		GetWidgetLocalCorners(widget, out lbCorner, out rtCorner);

		//		region.SetAnchor(obj, 0, 0, 0, 0);

		Vector3[] widgetWorldCorners = widget.worldCorners;
		region.transform.localPosition = (lbCorner + rtCorner) / 2;
		region.width = Mathf.FloorToInt(Mathf.Abs(lbCorner.x - rtCorner.x));
		region.height = Mathf.FloorToInt(Mathf.Abs(lbCorner.y - rtCorner.y));
		region.RemoveFromPanel();
		region.MarkAsChanged();
	}

	void UpdateControlRegion(Vector3 lbCorner, Vector3 rtCorner, bool fullBlock)
	{
		if (lbCorner == oldLbCorner && rtCorner == oldRtCorner)
		{
			return;
		}

		oldLbCorner = lbCorner;
		oldRtCorner = rtCorner;

		if (effectObj != null)
		{
			effectObj.transform.localPosition = (rtCorner + lbCorner) / 2;
			EffectSize effectSize = effectObj.GetComponent<EffectSize>();
			if (effectSize != null)
			{
				Vector3 size = rtCorner - lbCorner;
				size = effectObj.transform.TransformVector(size);

				size.x = Mathf.Abs(size.x);
				size.y = Mathf.Abs(size.y);



				ParticleSystem[] particles = effectSize.resizeParticles;
				for (int i = 0; i < particles.Length; i++)
				{
					ParticleSystem particle = particles[i];
                    particle.transform.localScale = new Vector3(size.x / effectSize.width, size.y / effectSize.height, 1);
				}
			}
		}
		else
		{
			controlRegion.transform.localPosition = (rtCorner + lbCorner) / 2;
//			float radius = (rtCorner - lbCorner).magnitude / 2;
			Vector3 a = rtCorner - lbCorner;
			a.x = Mathf.Abs(a.x);
			a.y = Mathf.Abs(a.y);
			a.z = Mathf.Abs(a.z);
			float radius = Mathf.Max(a.x, a.y) / 2;
			controlRegion.SetRegion(radius);

			if (fullBlock)
			{
				rtCorner = lbCorner;
			}
		}

		leftBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		leftBlock.rightAnchor.relative = 0.5f;
		leftBlock.rightAnchor.absolute = Mathf.CeilToInt(lbCorner.x);
		leftBlock.ResetAndUpdateAnchors();

		rightBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		rightBlock.leftAnchor.relative = 0.5f;
		rightBlock.leftAnchor.absolute = Mathf.FloorToInt(rtCorner.x);
		rightBlock.ResetAndUpdateAnchors();

		//		topBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		//		topBlock.leftAnchor.relative = 0.5f;
		//		topBlock.leftAnchor.absolute = Mathf.FloorToInt(lbCorner.x);
		//		topBlock.rightAnchor.relative = 0.5f;
		//		topBlock.rightAnchor.absolute = Mathf.CeilToInt(rtCorner.x);
		//		topBlock.bottomAnchor.relative = 0.5f;
		//		topBlock.bottomAnchor.absolute = Mathf.FloorToInt(rtCorner.y);
		//		topBlock.ResetAndUpdateAnchors();

		//		bottomBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		//		bottomBlock.leftAnchor.relative = 0.5f;
		//		bottomBlock.leftAnchor.absolute = Mathf.FloorToInt(lbCorner.x);
		//		bottomBlock.rightAnchor.relative = 0.5f;
		//		bottomBlock.rightAnchor.absolute = Mathf.CeilToInt(rtCorner.x);
		//		bottomBlock.topAnchor.relative = 0.5f;
		//		bottomBlock.topAnchor.absolute = Mathf.CeilToInt(lbCorner.y);
		//		bottomBlock.ResetAndUpdateAnchors();

		topBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		topBlock.leftAnchor.target = leftBlock.transform;
		topBlock.leftAnchor.relative = 1;
		topBlock.leftAnchor.absolute = 0;
		topBlock.rightAnchor.target = rightBlock.transform;
		topBlock.rightAnchor.relative = 0;
		topBlock.rightAnchor.absolute = 0;
		topBlock.bottomAnchor.relative = 0.5f;
		topBlock.bottomAnchor.absolute = Mathf.FloorToInt(rtCorner.y);
		topBlock.ResetAndUpdateAnchors();


		bottomBlock.SetAnchor(GetPanel().gameObject, 0, 0, 0, 0);
		bottomBlock.leftAnchor.target = leftBlock.transform;
		bottomBlock.leftAnchor.relative = 1;
		bottomBlock.leftAnchor.absolute = 0;
		bottomBlock.rightAnchor.target = rightBlock.transform;
		bottomBlock.rightAnchor.relative = 0;
		bottomBlock.rightAnchor.absolute = 0;
		bottomBlock.topAnchor.relative = 0.5f;
		bottomBlock.topAnchor.absolute = Mathf.CeilToInt(lbCorner.y);
		bottomBlock.ResetAndUpdateAnchors();
	}


}
